"use client";

import { likeApi } from '@/constants';
import { get, del } from '@/util/api';

// 点赞响应接口
export interface Like {
  id: number;
  userId: number;
  username?: string;
  avatar?: string;
  targetId: number;
  targetType: 'article' | 'comment';
  targetTitle?: string;
  targetContent?: string;
  createdAt: string;
}

// 点赞列表响应接口
export interface LikeListResponse {
  data: Like[];
  total: number;
  page: number;
  pageSize: number;
}

// 点赞统计数据接口
export interface LikeStats {
  totalCount: number;
  dailyTrend: Array<{
    date: string;
    count: number;
  }>;
  targetTypeDistribution: Array<{
    type: string;
    count: number;
    percentage: number;
  }>;
  topTargets: Array<{
    id: number;
    title: string;
    type: string;
    likeCount: number;
  }>;
  topUsers: Array<{
    userId: number;
    username: string;
    likeCount: number;
  }>;
}

/**
 * 点赞管理服务类
 * 封装所有点赞相关的API调用
 */
class LikeService {
  /**
   * 获取点赞记录列表
   * @param params 查询参数
   * @returns Promise<LikeListResponse>
   */
  async getLikeList(params?: {
    page?: number;
    pageSize?: number;
    userId?: number;
    targetId?: number;
    targetType?: 'article' | 'comment';
    startDate?: string;
    endDate?: string;
    orderBy?: 'createdAt';
    orderDirection?: 'asc' | 'desc';
  }): Promise<LikeListResponse> {
    try {
      const response = await get(likeApi.LIST, {params});
      return (response.data || response) as unknown as LikeListResponse;
    } catch (error) {
      console.error('获取点赞记录列表失败:', error);
      throw error;
    }
  }

  /**
   * 获取点赞详情
   * @param id 点赞记录ID
   * @returns Promise<Like>
   */
  async getLikeDetail(id: number): Promise<Like> {
    try {
      const response = await get(likeApi.DETAIL(id));
      return (response.data || response) as unknown as Like;
    } catch (error) {
      console.error(`获取点赞详情失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 获取指定文章的点赞记录
   * @param articleId 文章ID
   * @param params 查询参数
   * @returns Promise<LikeListResponse>
   */
  // async getArticleLikes(articleId: number, params?: {
  //   page?: number;
  //   pageSize?: number;
  //   startDate?: string;
  //   endDate?: string;
  // }): Promise<LikeListResponse> {
  //   try {
  //     const response = await get(likeApi.ARTICLE_LIKES(articleId), params);
  //     return response.data || response;
  //   } catch (error) {
  //     console.error(`获取文章点赞记录失败 (文章ID: ${articleId}):`, error);
  //     throw error;
  //   }
  // }

  /**
   * 获取指定评论的点赞记录
   * @param commentId 评论ID
   * @param params 查询参数
   * @returns Promise<LikeListResponse>
   */
  // async getCommentLikes(commentId: number, params?: {
  //   page?: number;
  //   pageSize?: number;
  //   startDate?: string;
  //   endDate?: string;
  // }): Promise<LikeListResponse> {
  //   try {
  //     const response = await get(likeApi.COMMENT_LIKES(commentId), params);
  //     return response.data || response;
  //   } catch (error) {
  //     console.error(`获取评论点赞记录失败 (评论ID: ${commentId}):`, error);
  //     throw error;
  //   }
  // }

  /**
   * 获取指定用户的点赞记录
   * @param userId 用户ID
   * @param params 查询参数
   * @returns Promise<LikeListResponse>
   */
  // async getUserLikes(userId: number, params?: {
  //   page?: number;
  //   pageSize?: number;
  //   targetType?: 'article' | 'comment';
  //   startDate?: string;
  //   endDate?: string;
  // }): Promise<LikeListResponse> {
  //   try {
  //     const response = await get(likeApi.USER_LIKES(userId), params);
  //     return response.data || response;
  //   } catch (error) {
  //     console.error(`获取用户点赞记录失败 (用户ID: ${userId}):`, error);
  //     throw error;
  //   }
  // }

  /**
   * 删除点赞记录
   * @param id 点赞记录ID
   */
  async deleteLike(id: number): Promise<void> {
    try {
      await del(likeApi.DELETE(id));
    } catch (error) {
      console.error(`删除点赞记录失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 批量删除点赞记录
   * @param ids 点赞记录ID数组
   * @returns Promise<{ success: number; failed: number }>
   */
  // async batchDeleteLikes(ids: number[]): Promise<{ success: number; failed: number }> {
  //   try {
  //     const response = await del(likeApi.BATCH_DELETE, { ids });
  //     return response.data || response;
  //   } catch (error) {
  //     console.error('批量删除点赞记录失败:', error);
  //     throw error;
  //   }
  // }

  /**
   * 获取点赞统计数据
   * @param params 查询参数
   * @returns Promise<LikeStats>
   */
  // async getLikeStats(params?: {
  //   startDate?: string;
  //   endDate?: string;
  //   limit?: number;
  // }): Promise<LikeStats> {
  //   try {
  //     const response = await get(likeApi.STATS, params);
  //     return response.data || response;
  //   } catch (error) {
  //     console.error('获取点赞统计数据失败:', error);
  //     throw error;
  //   }
  // }

  /**
   * 导出点赞记录
   * @param format 导出格式
   * @param params 查询参数
   * @returns Promise<Blob>
   */
  // async exportLikes(format: 'json' | 'csv' | 'excel' = 'json', params?: {
  //   userId?: number;
  //   targetType?: 'article' | 'comment';
  //   startDate?: string;
  //   endDate?: string;
  // }): Promise<Blob> {
  //   try {
  //     const response = await get(likeApi.EXPORT, { format, ...params }, {
  //       responseType: 'blob',
  //     });
  //     return response.data || response;
  //   } catch (error) {
  //     console.error('导出点赞记录失败:', error);
  //     throw error;
  //   }
  // }
}

// 导出点赞服务实例
export const likeService = new LikeService();
export default likeService;